
#include "../ui.h"
#include "../gui.h"
#include "../app_includes.h"

#define UI_FADE_IN_DELAY        (0)
#define HOMEPAGE_PAGE_NUM       (2)

typedef struct
{
    const char *name;
    const lv_img_dsc_t *icon;
    fun_app_open_t open;
    fun_app_close_t close;
    fun_app_group_select_t group_select;
    //uint8_t page_index;
}app_icons_data_t;

LV_IMG_DECLARE(ui_img_about_png);
LV_IMG_DECLARE(ui_img_cc3_png);
LV_IMG_DECLARE(ui_img_debug2_png);
LV_IMG_DECLARE(ui_img_power_png);
LV_IMG_DECLARE(ui_img_setting_png);

static app_icons_data_t app_data[] =
{
    {"CC表",         &ui_img_cc3_png,        lv_cc_open, lv_cc_close, cc_group_select, },
    {"电源",       &ui_img_power_png,  lv_dp_open, lv_dp_close, dp_group_select, },
    //{"串口",       &img_lv_icon_serial,    NULL, NULL,  NULL, },
    {"调试",       &ui_img_debug2_png,    lv_debug_open, lv_debug_close,  NULL, },
    {"设置",   &ui_img_setting_png,        lv_setting_open, lv_setting_close,  setting_group_select,},
    {"关于",   &ui_img_about_png,        lv_about_open, lv_about_close,  about_group_select, },
};

static ui_homepage_t *ui = &ui_homepage;

static lv_obj_t *ui_panel;
static lv_obj_t *ui_label_app_name;

static void homepage_init(void);
static void homepage_quit(lv_event_t *e);
static void homepage_icons_click(lv_event_t *e);

lv_obj_t *lv_homepage_load(void)
{
    //lv_obj_clean(ui->screen); //清屏

    ui_main_screen.home_flag = 1;
    ui->screen = lv_screen_create(ui_main_screen.desktop, LV_HOR_RES, LV_VER_RES);

/*
    LV_IMG_DECLARE(ui_img_bg_png);
    ui->bg = lv_img_create(ui->screen);
    lv_img_set_src(ui->bg, &ui_img_bg_png);
    lv_obj_align(ui->bg, LV_ALIGN_CENTER,0,0);
*/
    homepage_init();

    //lv_obj_add_event_cb(ui->screen, homepage_quit, LV_EVENT_SCREEN_UNLOADED, NULL);

    return ui->screen;
}

static void homepage_quit(lv_event_t *e)
{
    lv_event_code_t event_code = lv_event_get_code(e);
    if(event_code==LV_EVENT_SCREEN_UNLOADED) {
          ;//lv_obj_del(ui->screen);
    }
}

void homepage_icons_click(lv_event_t *e)
{
    lv_obj_t *screen;
    app_icons_data_t * app = (app_icons_data_t *)(e->user_data);
    if(app->open!=NULL) {
        ui_main_screen.home_flag = 0;
        lv_group_remove_all_objs(g_encoder);
        lv_obj_del(ui->screen);
        ui_main_screen.current_desktop = lv_screen_create(ui_main_screen.desktop, LV_HOR_RES, LV_VER_RES);
        ui_main_screen.app_tabview = app->open(ui_main_screen.current_desktop);
        ui_main_screen.group_select = app->group_select;
        ui_main_screen.app_close = app->close;
        ui_main_screen.dis_dir = app_dev.para.store.display_dir;
        //lv_scr_load_anim(screen, LV_SCR_LOAD_ANIM_OUT_LEFT, 100, 0, false);
    }
}

/*
void homepage_icons_select_next(int16_t cnt)
{
    cnt += ui->app_select;
    if(cnt<0) cnt = UI_HOMEPAGE_APP_NUM-1;
    else if(cnt>=UI_HOMEPAGE_APP_NUM) cnt = 0;
    select_Animation(ui->app_icons[ui->app_select], -20);
    ui->app_select = cnt;
    select_Animation(ui->app_icons[ui->app_select], 20);
}
*/
static void homepage_add_icons(void)
{
    uint8_t i;
    lv_obj_t *app_panel;
    lv_obj_t *app_name;
    lv_coord_t col_space = 40;
    lv_coord_t row_space = 40;
    uint16_t icon_size = 60;
    for(i=0;i!=UI_HOMEPAGE_APP_NUM;i++) {
        app_panel = lv_obj_create(ui_panel);
        lv_obj_set_size(app_panel, 66, 86);
        lv_obj_clear_flag(app_panel, LV_OBJ_FLAG_SCROLLABLE);
        lv_obj_set_style_border_width(app_panel, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
        lv_obj_set_style_bg_color(app_panel, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT);
        lv_obj_set_style_bg_color(app_panel, lv_color_hex(0x292831), LV_PART_MAIN | LV_STATE_FOCUSED);
        lv_obj_set_style_border_width(app_panel, 0, LV_PART_MAIN | LV_STATE_DEFAULT);
        lv_obj_add_flag(app_panel, LV_OBJ_FLAG_CLICKABLE);
        lv_obj_add_event_cb(app_panel, homepage_icons_click, LV_EVENT_CLICKED, &app_data[i]);
        //lv_obj_align(app_panel, LV_ALIGN_LEFT_MID, 100+i*80, -20);
        
        ui->app_panel[i] = app_panel;
            
        lv_group_add_obj(g_encoder,ui->app_panel[i]);
        ui->app_icons[i] = lv_img_create(ui->app_panel[i]);
        lv_img_set_src(ui->app_icons[i],app_data[i].icon);
        lv_obj_align(ui->app_icons[i], LV_ALIGN_TOP_MID, 0, -5);
        
        app_name = lv_label_create(app_panel);
        lv_obj_set_style_text_font(app_name, &ui_font_FontChineseSmall, LV_PART_MAIN | LV_STATE_DEFAULT);
        lv_label_set_text(app_name, app_data[i].name);
        lv_obj_align_to(app_name, ui->app_icons[i], LV_ALIGN_OUT_BOTTOM_MID, 0, 2);
        
    }
}

static void homepage_init(void)
{
    lv_style_t row_container_style;
    ui_panel = lv_screen_create(ui->screen, LV_HOR_RES, 220);
    lv_obj_align(ui_panel, LV_ALIGN_CENTER, 0, 20);

    lv_obj_add_flag(ui_panel, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_flex_flow(ui_panel, LV_FLEX_FLOW_ROW_WRAP);
    lv_obj_set_flex_align(ui_panel, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);

    //lv_obj_set_style_pad_row(ui_panel, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    //lv_obj_set_style_pad_column(ui_panel, 0, LV_PART_MAIN| LV_STATE_DEFAULT);
    
    homepage_add_icons();

}
